package com.youkeyi.ddy.cloud.basic.crm.config.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.crm.config.entity.SysCustomerConfig;
import com.youkeyi.ddy.cloud.basic.crm.config.service.SysCustomerConfigService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.config.CrmCustomerConfigCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.config.dto.CrmCustomerConfigListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.config.dto.CrmCustomerConfigListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.config.param.CrmCustomerConfigCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.config.param.CrmCustomerConfigQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.config.param.CrmCustomerConfigUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 客户释放配置表 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-04-01
 */
@Service
public class SysCustomerConfigController implements CrmCustomerConfigCloudService {

    @Autowired
    private SysCustomerConfigService sysCustomerConfigService;

    @Override
    public CrmCustomerConfigListCloudDto query(CrmCustomerConfigQueryCloudParam param) {
        CrmCustomerConfigListCloudDto dto = new CrmCustomerConfigListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getCorpKeys())) {
            param.getCorpKeys().removeAll(Collections.singleton(null));
        }

        QueryWrapper<SysCustomerConfig> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getCorpKeys()), SysCustomerConfig::getCorpKey, param.getCorpKeys())
                .ne(SysCustomerConfig::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;

        Page<SysCustomerConfig> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<SysCustomerConfig> customerConfigList = sysCustomerConfigService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(customerConfigList.getRecords())) {
            dto.setTotal(customerConfigList.getTotal());

            List<CrmCustomerConfigListItemCloudDto> items = new ArrayList<>();
            customerConfigList.getRecords().forEach(customerConfig -> {
                CrmCustomerConfigListItemCloudDto item = new CrmCustomerConfigListItemCloudDto();
                item.setCustomerConfigPrimaryKey(customerConfig.getPrimaryKey());
                item.setCorpKey(customerConfig.getCorpKey());
                item.setIsOpen(customerConfig.getIsOpen());
                item.setReleaseFrequently(customerConfig.getReleaseFrequently());
                item.setRemark(customerConfig.getRemark());
                item.setIsDeleted(customerConfig.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(customerConfig.getCreateTime() != null ? customerConfig.getCreateTime().getTime() : null);
                item.setCreateUserKey(customerConfig.getCreateUserKey());
                item.setCreateUserName(customerConfig.getCreateUserName());
                item.setUpdateTime(customerConfig.getUpdateTime() != null ? customerConfig.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(customerConfig.getUpdateUserKey());
                item.setUpdateUserName(customerConfig.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(CrmCustomerConfigCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param
                || StringUtils.isBlank(param.getCorpKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        SysCustomerConfig customerConfig = new SysCustomerConfig();
        customerConfig.setPrimaryKey(SimpleUUidUtils.getUuid());
        customerConfig.setCorpKey(param.getCorpKey());
        customerConfig.setIsOpen(param.getIsOpen());
        customerConfig.setReleaseFrequently(param.getReleaseFrequently());
        customerConfig.setRemark(param.getRemark());
        customerConfig.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        customerConfig.setCreateUserKey(param.getCreateUserKey());
        customerConfig.setCreateUserName(param.getCreateUserName());
        customerConfig.setCreateTime(new Date());
        boolean save = sysCustomerConfigService.save(customerConfig);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(customerConfig.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(CrmCustomerConfigUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getCustomerConfigPrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        UpdateWrapper<SysCustomerConfig> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(SysCustomerConfig::getPrimaryKey, param.getCustomerConfigPrimaryKey());
        SysCustomerConfig customerConfig = new SysCustomerConfig();
        customerConfig.setPrimaryKey(param.getCustomerConfigPrimaryKey());
        customerConfig.setIsOpen(param.getIsOpen());
        customerConfig.setReleaseFrequently(param.getReleaseFrequently());
        customerConfig.setRemark(param.getRemark());
        customerConfig.setUpdateUserKey(param.getUpdateUserKey());
        customerConfig.setUpdateUserName(param.getUpdateUserName());
        customerConfig.setUpdateTime(new Date());
        boolean save = sysCustomerConfigService.update(customerConfig, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        return rc;
    }
}
